package com.bilibili.juc.me;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j(topic = "c.ThreadLocalDemo")
public class ThreadLocalDemo {
    static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(()-> 0);
    static ThreadLocal<Integer> threadLocalTwo = ThreadLocal.withInitial(()-> 12);
    static Random random = new Random();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            int num = random.nextInt();
            log.debug("num:{}", num);
            threadLocal.set(num);
            num = random.nextInt();
            log.debug("num two:{}", num);
            threadLocalTwo.set(num);
            log.debug("over" + threadLocal.get());
            log.debug("over two" + threadLocalTwo.get());
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            int num = random.nextInt();
            log.debug("num:{}", num);
            threadLocal.set(num);
            num = random.nextInt();
            log.debug("num two:{}", num);
            threadLocalTwo.set(num);
            log.debug("over" + threadLocal.get());
            log.debug("over two" + threadLocalTwo.get());
        }, "t2");
        t2.start();
        log.debug("main over");
    }
}
